package cn.itcast.bos.action;

import java.io.IOException; 
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;

import com.opensymphony.xwork2.ActionSupport; 
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.PageBean;
import cn.itcast.bos.domain.User;
import cn.itcast.bos.service.IDecidezoneService;
import cn.itcast.bos.service.IFunctionService;
import cn.itcast.bos.service.INoticebillService;
import cn.itcast.bos.service.IRoleService;
import cn.itcast.bos.service.IUserService;
import cn.itcast.bos.service.IWorkordermanageService;
import cn.itcast.bos.service.region.IRegionService;
import cn.itcast.bos.service.staff.IStaffService;
import cn.itcast.bos.service.subarea.ISubareaService;
import cn.itcast.crm.service.CustomerService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {

	protected T model;
	public T getModel() {
		return model;
	}
	
	@Resource
	protected ISubareaService subareaService;
	@Resource
	protected IUserService UserService;
	@Resource
	protected IStaffService staffService;
	@Resource
	protected IRegionService regionService;
	@Resource
	protected IDecidezoneService decidezoneService;
	@Resource
	protected CustomerService customerService;
	@Resource
	protected INoticebillService noticebillService;
	@Resource
	protected IWorkordermanageService workordermanageService;
	@Resource
	protected IFunctionService functionService;
	@Resource
	protected IRoleService roleService;
	
	private int page;
	private int rows;
	public void setPage(int page) {
		pageBean.setCurrentPage(page);
	}
	public void setRows(int rows) {
		pageBean.setPageSize(rows);
	}

	/*public void setPage(int page){
		pageBean.setCurrentPage(page);
	}
	public void setRows(int rows){
		pageBean.setPageSize(rows);
	}*/
	protected PageBean pageBean = new PageBean();
	protected DetachedCriteria detachedCriteria;
	public BaseAction() {
		//获得父类BaseAction<T>的类型
		ParameterizedType genericSuperclass = null;
		Type genericSuperclass2 = this.getClass().getGenericSuperclass();
		
		if (genericSuperclass2 instanceof ParameterizedType) {
			genericSuperclass = (ParameterizedType) genericSuperclass2;
		} else {
			genericSuperclass = (ParameterizedType) this.getClass().getSuperclass().getGenericSuperclass();
		}
		Type[] types = genericSuperclass.getActualTypeArguments();
		//实体类型
		Class<T> t =  (Class<T>) types[0];
		
		//在构造方法中创建动态离线查询对象
		detachedCriteria = DetachedCriteria.forClass(t);
		pageBean.setDetachedCriteria(detachedCriteria);
		try {
			model=t.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	
	//将pageBean对象序列化为json,返回浏览器端
	public void writePageBeanToJson(PageBean pageBean,String[] excludes){
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(excludes);
		JSONObject jsonObject = JSONObject.fromObject(pageBean, jsonConfig);
		String json = jsonObject.toString();
		
		//将json数据通过输出流写会数据段
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		try {
			ServletActionContext.getResponse().getWriter().print(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//将list对象序列化为json,返回浏览器端
		public void writeListToJson(List list,String[] excludes){
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.setExcludes(excludes);
			 JSONArray array = JSONArray.fromObject(list, jsonConfig);
			String json = array.toString();
			
			//将json数据通过输出流写会数据段
			ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
			try {
				ServletActionContext.getResponse().getWriter().print(json);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	
	
	//返回浏览器端的json
	public void getBackJson(String json){
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		try {
			ServletActionContext.getResponse().getWriter().print(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//将实体类对象序列化为json数据
	public void writeObject2json(Object object, String[] excludes) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(excludes);
		JSONObject jsonObject = JSONObject.fromObject(object, jsonConfig);
		String json = jsonObject.toString();
		// 将json数据通过输出流写到客户端
		ServletActionContext.getResponse().setContentType(
				"text/json;charset=UTF-8");
		try {
			ServletActionContext.getResponse().getWriter().print(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
